package org.shoukaiseki.common.utils

import org.apache.commons.lang3.StringUtils
import java.lang.Exception
import java.lang.IllegalArgumentException

object EnumUtils {

    @JvmStatic
    fun <T> getDictDataEnumByValue(clazz: Class<T>?, value: Any): T {
        if (clazz == null || !clazz.isEnum) {
            throw IllegalArgumentException("class为空或者不是枚举类")
        } else {
            clazz.isEnum
            val enums = clazz.enumConstants
            for (anEnum in enums) {
                try {
                    val method = clazz.getMethod("getValue")
                    val invoke = method.invoke(anEnum)
                    if (StringUtils.equals(invoke.toString(), value.toString())) {
                        return anEnum
                    }
                } catch (e: Exception) {
                    throw IllegalArgumentException("枚举无value属性")
                }
            }
        }
        throw IllegalArgumentException("不存在value对应的枚举")
    }
}